import { wxRequest} from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    deviceList:[],
    imgUrl: app.globalData.imgUrl,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    devId: null,
    currentDevice: {},
    cententDetail: {}
  },

  
  onLoad: function (options) {
    if(options.devId) {
      this.setData({
        devId: options.devId
      })
    }
    this.getlist();
  },
 
// 获取列表
getlist: function () {
  var that = this
  _wxRequest.wxTonkenAjax({
    url: app.globalData.apiUrl + "/org/protected/bizOrg/findStoreList",
    method: "post",
    data: {
      keyword: that.data.keywords,
      status: 1,
      pageSize: 10000
    }
  }, function (res) {
    if(res.list && res.list.length > 0) {
      that.setData({
        storeItem: res.list[0]
      })
    }
    
  })
},
 
  onShow: function () {
    this.getDeviceList()
  },
  handleStore: function() {
    wx.navigateTo({
      url: '/pages/publish/template/store',
    })
  },



  scanQrCode(e) {
    wx.scanCode({
      onlyFromCamera: true,
      scanType: ["barCode", "qrCode"],
      success: (res) => {
        if(res.errMsg == "scanCode:ok") {
          let barCode = res.result;
          this.setData({
            ["cententDetail.code"]: barCode
          })
        }
      }
    })
  },


  //查询列表
  getDeviceList: function() {
    var params = {
      keyword: this.data.keyword, 
      pageNo: this.data.pageNo,
      pageSize:30
    }
     
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/device/findList',
      method: 'POST',
      data:params
    }, (res) => {
      let list = res.list;
      let currentDevice = res.list.filter(item => item.id == this.data.devId)
      let cententDetail = currentDevice.length > 0? currentDevice[0]:{}
      this.setData({
        deviceList: list,
        currentDevice,
        cententDetail,
        pageNo: res.page.pageNo,
        hasNextPage: res.page.hasNextPage,
      });
    })
  },

  bindPickerChange:function(e){
    var val=e.detail.value
    this.setData({
      typeIndex:val
    })  
    console.log(e,'bindPickerChange')
  },





  
  //表单提交
  formSubmit: function(e) {
    var formdata = e.detail.value; 

    // if (!this.data.typeIndex) {
    //   wx.showToast({
    //     icon: 'none',
    //     title: '选择绑定设备',
    //   })
    //   return
    // }   
  

    if (!this.data.storeItem.stoName) {
      wx.showToast({
        icon: 'none',
        title: '请选择绑定门店',
      })
      return
    }

    if (this.data.cententDetail.code == '' || !this.data.cententDetail.code) {
      wx.showToast({
        icon: 'none',
        title: '设备编码错误',
      })
      return
    }
    formdata.id = this.data.devId;
    formdata.code = this.data.currentDevice.code;
    formdata.stoId=this.data.storeItem.id
    //formdata.typeId=this.data.deviceList[this.data.typeIndex].id

    console.log(formdata,'formdata') 
    this.formRequest(formdata);
  },




  //请求
  formRequest: function(formdata) {
    var url = '/gds/protected/device/update-bind';
    var title = "修改成功"
    //修改
    // if (this.data.id) {
    //   url = "/gds/protected/comm/beanCrud/iot.DeviceTypeVO/update";
    //   formdata.id = this.data.cententDetail.id;
    //   title = "修改绑定成功"
    // }  

    if(formdata.note == '' || (!formdata.note)) {
      wx.showToast({
        title: '请填写设备标签',
        icon: 'none'
      })
      return ;
    }

    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'POST',
      data: formdata
    }, (res) => {
      console.log(res)
      wx.showToast({
        title,
      })
      setTimeout(() => {
        wx.switchTab({  
          url: '/pages/publish/bind_device/list',
        })
      }, 1500)

    })
  },





})